package com.jerome.fs.server.aggregation.handler;

import com.jerome.fs.entities.WeatherVo;
import com.jerome.fs.http.HttpMethodEnum;
import com.jerome.fs.server.aggregation.ContextUtil;
import com.jerome.fs.sync.SyncClientHandler;
import com.jerome.fs.sync.SyncVo;
import com.jerome.fs.storage.ExpiringMap;

import java.util.List;

/**
 * 从节点同同步类
 * @author Jerome
 * @date 2023/9/12
 */
public class SlaveSyncClientHandler implements SyncClientHandler {
    @Override
    public void handle(SyncVo o) {
        Object content = o.getContent();
        //更新lamport时钟
        if (o.getLatestTime() > ContextUtil.getLamportClock().getLatestTime()) {
            ContextUtil.getLamportClock().tick(o.getLatestTime());
        }
        HttpMethodEnum methodEnum = HttpMethodEnum.getByCode(o.getMethod());
        switch (methodEnum){
            case INCREMENT_SYNC:
                //增量同步
                List<WeatherVo> weatherVos = (List<WeatherVo>) content;
                //更新
                ContextUtil.updateExpiringMap(weatherVos);
                break;
            case SYNC:
                //全量同步
                ExpiringMap<String, WeatherVo> expiringMap = (ExpiringMap<String, WeatherVo>) content;
                //更新
                ContextUtil.updateExpiringMap(expiringMap);
                break;
            case REGISTER:
                //注册
                ContextUtil.updateNodeMap((ExpiringMap<String, String>) content);
                break;
            default:
                break;
        }

    }
}
